package com.youshang520i.consuldiscoveryconsumer;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.experimental.ExtensionMethod;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Random;

/**
 * @author youshang
 */
@Log4j2
@RestController
@SpringBootApplication
@EnableFeignClients//开启feign的功能。
@EnableHystrix// 开启hystrix的功能。
public class ConsulDiscoveryConsumer {

    public static void main(String[] args) {
        SpringApplication.run(ConsulDiscoveryConsumer.class, args);
    }

    @Bean
    public MessageConverter jsonMessageConverter(ObjectMapper objectMapper) {
        return new Jackson2JsonMessageConverter(objectMapper);
    }





    //重点： Value 不会起到针对Consul动态加载配置文件的作用
    @Value("${spring.rabbitmq.host}")
    public String env1;

//    @PostConstruct
//    public void init(){
//        log.info("env1:  " + env1);
//    }


    @Bean
    @LoadBalanced
    @Qualifier("loadBalancedRestTemplate")
    public RestTemplate loadBalancedRestTemplate() {
        return new RestTemplate();
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    // 1. 使用带负载均衡功能的 RestTemplate 访问服务
    @Autowired
    @Qualifier("loadBalancedRestTemplate")
    private RestTemplate loadBalancedRestTemplate;

    @Autowired
    @Qualifier("restTemplate")
    private RestTemplate restTemplate;

    // 2. 使用 DiscoveryClient 访问服务
    @Autowired
    private DiscoveryClient discoveryClient;

    private static final String CONSUL_DISCOVERY_PROVIDER_NAME = "consul-discovery-provider";
    private static final String CONSUL_DISCOVERY_PROVIDER_URL = "http://consul-discovery-provider/init";

    @RequestMapping("/loadBalancedRestTemplate")
    public String invokeWithRestTemplate() throws InterruptedException {
        return loadBalancedRestTemplate.getForObject(CONSUL_DISCOVERY_PROVIDER_URL, String.class);
    }

    @RequestMapping("/discoveryClient")
    public String invokeWithDiscoveryClient() throws Exception {
        List<ServiceInstance> instances = discoveryClient.getInstances(CONSUL_DISCOVERY_PROVIDER_NAME);
        log.info("serviceList -> {}", instances);
        String serviceUrl = instances.get(new Random().nextInt(instances.size())).getUri().toURL().toString();
        log.info("serviceUrl -> {}", serviceUrl);
        return restTemplate.getForObject(serviceUrl+"/init", String.class);
    }













}
